#include "clueCardPlayStatusResMessage.h"
#include "jsonUtils.h"

ClueCardPlayStatusResMessage::ClueCardPlayStatusResMessage(MtpProtocol* packet) : MtpMessage(packet), clueCardId(0), channel(0), duration(0), position(0), volume(0)
{
}

ClueCardPlayStatusResMessage::ClueCardPlayStatusResMessage(const int& version, const uint32_t& commandId, const int64_t& clueCardId, 
	const int& channel, const int& duration, const int& position, const int& volume)
	: MtpMessage(new MtpProtocol(L"clueCardPlayStatusRes",version,commandId)),res(true),clueCardId(clueCardId),channel(channel),duration(duration),position(position),volume(volume)
{
}

void ClueCardPlayStatusResMessage::decodeBody()
{
	MtpMessage::decodeBody();
	if (!jsonContent.empty()) {
		JsonDoc& doc = res.jsonDeserialize(jsonContent);
		const JsonValue::ConstMemberIterator& it = doc.FindMember(L"data");
		if (it != doc.MemberEnd() && it->value.IsObject()) {
			JsonUtils::loadLong(it->value, L"clueCardId", clueCardId);
			JsonUtils::loadInt(it->value, L"channel", channel);
			JsonUtils::loadInt(it->value, L"duration", duration);
			JsonUtils::loadInt(it->value, L"position", position);
			JsonUtils::loadInt(it->value, L"volume", volume);
		}
	}
}

void ClueCardPlayStatusResMessage::encodeBody()
{
	JsonDoc& doc = res.jsonSerialize();
	JsonValue v(rapidjson::kObjectType);
	
	v.AddMember(L"clueCardId", clueCardId, doc.GetAllocator());
	v.AddMember(L"channel", channel, doc.GetAllocator());
	v.AddMember(L"duration", duration, doc.GetAllocator());
	v.AddMember(L"position", position, doc.GetAllocator());
	v.AddMember(L"volume", volume, doc.GetAllocator());

	doc.AddMember(L"data", v, doc.GetAllocator());
	jsonContent = JsonUtils::jsonStr(doc);
	MtpMessage::encodeBody();
}
